/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_hdi_test_entry.h"
#include "sle_adapter_hdi.h"
#include "sle_connect_hdi.h"
#include "sle_unit_test_log.h"

SleConnectHdiCallbacks *g_connectHdiUtCb;
SleConnectHdiOpts *g_connectHdiUtOpts = NULL;

void OnSleHdiUtConnectStateChangedCallback(uint16_t connectId, const SleDeviceAddress *addr,
    SleAcbState connState, SlePairState pairState, SleDisConnectReason discReason)
{
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]OnSleHdiUtConnectStateChangedCallback");
}

void OnSleHdiUtConnectParamUpdatedCallback(uint16_t connectId, SleHdiErrorCode status,
    const SleConnectionParamUpdateEvt *param)
{
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]OnSleHdiUtConnectParamUpdatedCallback");
}

void OnSleHdiUtRequestConnectParamUpdateCallback(uint16_t connectId, SleHdiErrorCode status,
    const SleConnectParam *param)
{
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]OnSleHdiUtRequestConnectParamUpdateCallback");
}

void OnSleHdiUtAuthCompletedCallback(uint16_t connectId, const SleDeviceAddress *addr, SleHdiErrorCode status,
    const SleAuthInfo* evt)
{
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]OnSleHdiUtAuthCompletedCallback");
}

void OnSleHdiUtPairCompletedCallback(uint16_t connectId, const SleDeviceAddress *addr, SleHdiErrorCode status)
{
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]OnSleHdiUtPairCompletedCallback");
}

void OnSleHdiUtReadRssiCallback(uint16_t connectId, int8_t rssi, SleHdiErrorCode status)
{
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]OnSleHdiUtReadRssiCallback");
}

void GetSleConnectHdiOptsTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = GetSleConnectHdiOpts(g_connectHdiUtOpts);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] GetSleConnectHdiOptsTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]GetSleConnectHdiOptsTestLevel0");
}

void SleHdiRegisterConnectCallbacksTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_connectHdiUtOpts->registerConnectCbks(g_connectHdiUtCb);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiRegisterConnectCallbacksTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiRegisterConnectCallbacksTestLevel0");
}

void SleHdiConnectRemoteTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress addr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
    };
    ret = g_connectHdiUtOpts->connectRemote(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiConnectRemoteTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiConnectRemoteTestLevel0");
}

void SleHdiUpdateConnectParamTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleConnectionParamUpdate params = {
        .connectId = 0,
        .intervalMin = 0x64,
        .intervalMax = 0x64,
        .maxLatency = 0,
        .supervisionTimeout = 0x1F3,
    };
    ret = g_connectHdiUtOpts->updateConnectParam(&params);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiUpdateConnectParamTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiUpdateConnectParamTestLevel0");
}

void SleHdiPairRemoteTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress addr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
    };
    ret = g_connectHdiUtOpts->sendPairReq(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiPairRemoteTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiPairRemoteTestLevel0");
}

void SleHdiGetPairedDevicesNumTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    uint16_t number = 0;
    ret = g_connectHdiUtOpts->getPairedDevicesNum(&number);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiGetPairedDevicesNumTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiGetPairedDevicesNumTestLevel0");
}

void SleHdiGetPairedDevicesTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress devAddr[10];
    SleDeviceAddress addr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
    };
    ret = g_connectHdiUtOpts->removePairDevice(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiGetPairedDevicesTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiGetPairedDevicesTestLevel0");
}

void SleHdiGetPairStateTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress addr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
    };
    SlePairState state;
    ret = g_connectHdiUtOpts->getPairState(&addr, &state);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiGetPairStateTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiGetPairStateTestLevel0");
}

void SleHdiReadRemoteDeviceRssiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    uint16_t connectId = 0;
    ret = g_connectHdiUtOpts->readRemoteRssi(connectId);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiReadRemoteDeviceRssiTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiReadRemoteDeviceRssiTestLevel0");
}

void SleHdiRemovePairedDeviceTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress addr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
    };
    ret = g_connectHdiUtOpts->removePairDevice(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiRemovePairedDeviceTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiRemovePairedDeviceTestLevel0");
}

void SleHdiRemoveAllPairsTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_connectHdiUtOpts->removeAllPairs();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiRemoveAllPairsTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiRemoveAllPairsTestLevel0");
}

void SleHdiDisconnectRemoteTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress addr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}
    };
    ret = g_connectHdiUtOpts->disconnectRemote(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleConnectHdiTest][FAILED] SleHdiDisconnectRemoteTestLevel0  failed(%08X)",ret);
    SLE_UT_LOG("[SleConnectHdiTest][PASSED]SleHdiDisconnectRemoteTestLevel0");
}

void SleConnectHdiTestLevel0(void)
{
    GetSleConnectHdiOptsTestLevel0();
    SleHdiRegisterConnectCallbacksTestLevel0();
    SleHdiConnectRemoteTestLevel0();
    SleHdiUpdateConnectParamTestLevel0();
    SleHdiPairRemoteTestLevel0();
    SleHdiGetPairedDevicesNumTestLevel0();
    SleHdiGetPairedDevicesTestLevel0();
    SleHdiGetPairStateTestLevel0();
    SleHdiReadRemoteDeviceRssiTestLevel0();
    SleHdiRemovePairedDeviceTestLevel0();
    SleHdiRemoveAllPairsTestLevel0();
    SleHdiDisconnectRemoteTestLevel0();
}

SleHdiErrorCode InitSleConnectHdiTestEntry(void)
{
    g_connectHdiUtOpts = (SleConnectHdiOpts *)malloc(sizeof(SleConnectHdiOpts));
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_connectHdiUtOpts != NULL, SLE_HDI_ERRCODE_FAIL, "[SleConnectHdiTest][FAILED]InitSleConnectHdiTestEntry malloc failed");
    g_connectHdiUtCb = (SleConnectHdiCallbacks *)malloc(sizeof(SleConnectHdiCallbacks));
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_connectHdiUtCb != NULL, SLE_HDI_ERRCODE_FAIL, "[SleConnectHdiTest][FAILED]InitSleConnectHdiTestEntry malloc failed");
    g_connectHdiUtCb->OnConnectStateChangedCb = OnSleHdiUtConnectStateChangedCallback;
    g_connectHdiUtCb->OnConnectParamUpdateReqCb = OnSleHdiUtRequestConnectParamUpdateCallback;
    g_connectHdiUtCb->OnConnectParamUpdateCb = OnSleHdiUtConnectParamUpdatedCallback;
    g_connectHdiUtCb->OnAuthCompleteCb = OnSleHdiUtAuthCompletedCallback;
    g_connectHdiUtCb->OnPairCompleteCb = OnSleHdiUtPairCompletedCallback;
    g_connectHdiUtCb->OnReadRssiCb = OnSleHdiUtReadRssiCallback;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode DeinitSleConnectHdiTestEntry(void)
{
    free(g_connectHdiUtCb);
    free(g_connectHdiUtOpts);
    return SLE_HDI_ERRCODE_SUCC;
}

void SleConnectHdiTestEntry(void)
{
    SLE_UT_LOG("__SleConnectHdiTestEntry start");
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = InitSleConnectHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HAL_SUCCESS);
    SleConnectHdiTestLevel0();
    // SleConnectHdiTestLevel1();
    ret = DeinitSleConnectHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HAL_SUCCESS);
    SLE_UT_LOG("__SleConnectHdiTestEntry end");
}

